<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: loginlog.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");
include("top.php");
?>
<h1>Deltakerinnlogginger</h1>
<?
if ($_REQUEST['peruser'] == 'true') {
	?>
	<a href="loginlog.php">Vis 100 siste</a>
	<p>
	<table class="gridded">
	<?
	$sql="SELECT * FROM deltakere WHERE LaivID=$laivid ORDER BY Navn";
	$deltakere=mysql_query($sql) or die(mysql_error());
	while ($deltaker=mysql_fetch_assoc($deltakere)) {
		$sql="SELECT DATE_FORMAT(tidspunkt, '%Y-%m-%d %H:%i'), ROUND((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(tidspunkt))/3600) AS timer FROM loginlog WHERE deltakerID=".$deltaker['ID']." ORDER BY tidspunkt DESC";
		$logins=mysql_query($sql) or die(mysql_error());
		if (mysql_num_rows($logins)) {
			$login=mysql_fetch_row($logins);
			$text=$login[0];
			$text2=$login[1]." timer siden";
			if ($login[1] <= 1) {
				$text="<font color=\"red\">$text</font>";
				$text2="<font color=\"red\">$text2</font>";
			}
		} else {
			$text="Aldri logget inn";
			$text2="--";
		}
		?>
		<tr>
		<td><?=$deltaker['Navn']?></td>
		<td><?=$text?></td>
		<td align="right"><?=$text2?></td>
		</tr>
		<?
	}
	?>
	</table>
	<?
} else {
	?>
	<a href="loginlog.php?peruser=true">Vis brukeres siste innlogging</a>
	<p>
	<?
	$sql="SELECT navn, DATE_FORMAT(tidspunkt, '%Y-%m-%d %H:%i'), ROUND((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(tidspunkt))/3600) AS timer FROM loginlog LEFT JOIN deltakere ON loginlog.deltakerID=deltakere.ID WHERE LaivID=$laivid ORDER BY tidspunkt DESC LIMIT 100";
	$logins=mysql_query($sql);
	echo "<table class=\"gridded\"><tr><td valign=\"top\">";
	echo "<table style=\"border-collapse:collapse;\">";
	while ($login=mysql_fetch_row($logins)) {
		if ($bg == "primbg") $bg="secbg"; else $bg="primbg";
		?>
		<tr style="border-bottom: 1px solid black">
		<td class="<?=$bg?>"><?=$login[0]?></td>
		<td class="<?=$bg?>"><?=$login[1]?></td>
		<td class="<?=$bg?>" style="text-align:right"><?=$login[2]?> timer siden</td>
		</tr>
		<?
	}
	echo "</table></td>";

	$sql = "SELECT HOUR(tidspunkt) as time, COUNT(*) as antall FROM loginlog LEFT JOIN deltakere ON loginlog.deltakerID=deltakere.ID WHERE LaivID=$laivid GROUP BY HOUR(tidspunkt)";
	$timer = mysql_query($sql);
	echo "<td valign=\"top\"><table class=\"gridded\">";
	while ($time = mysql_fetch_assoc($timer)) {
		?>
		<tr>
		<td>Kl. <?=$time['time']?>:00 - <?=$time['time']?>:59</td>
		<td align="right"><?=$time['antall']?></td>
		</tr>
		<?
	}

	$sql = "SELECT DATE_FORMAT(tidspunkt, '%d. %M') as dagen, COUNT(*) as antall FROM loginlog LEFT JOIN deltakere ON loginlog.deltakerID=deltakere.ID WHERE LaivID=$laivid GROUP BY DAYOFYEAR(tidspunkt)";
	$timer = mysql_query($sql) or die(mysql_error());
	echo "<table class=\"gridded\">";
	while ($time = mysql_fetch_assoc($timer)) {
		?>
		<tr>
		<td><?=$time['dagen']?></td>
		<td align="right"><?=$time['antall']?></td>
		</tr>
		<?
	}
	echo "</table></td></tr></table>";
}
include("bottom.php");
?>
